﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Builders;

namespace DevMango_Core
{
    class Connection
    {
        public void connectServer(String Server, int Port, String Database, String UserName, String Password, frmServerExplorer frmSE, bool admin)
        {
            bool badmin;
            MongoConnectionStringBuilder mConString = new MongoConnectionStringBuilder();
            
            MongoServerAddress mServerAddress = new MongoServerAddress(Server, Port);
            if (admin || Database == "admin")
            {
                badmin = true;
            }
            else
            {
                badmin = false;
            }

            MongoCredentials mCredential = new MongoCredentials(UserName,Password, badmin);
                     
            mConString.Server = mServerAddress;
            mConString.DatabaseName = Database;
            
            

            MongoServer mServer = MongoServer.Create(mConString);
            try
            {
                mServer.Connect();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

            TreeNode tnServer = new TreeNode();

            tnServer.Text = Server;

            tnServer.ImageIndex = 0;
            
            
            IEnumerable<String> databases = mServer.GetDatabaseNames();
            foreach (string db in databases)
            {
                TreeNode tnDB = new TreeNode();
                tnDB.Text = db;
                tnDB.ImageIndex = 1;

                TreeNode tnCol = new TreeNode();
                tnCol.Text = "Collections";
                tnCol.ImageIndex = 2;

                mConString.DatabaseName = db;
                mServer = MongoServer.Create(mConString);

                MongoDatabase mongodb = mServer.GetDatabase(db);
                try
                {
                    mServer.Connect();
                }
                catch 
                {
                }

                IEnumerable<String> collections = mServer.GetDatabase(db).GetCollectionNames();
                
                foreach (string cols in collections)
                {
                    TreeNode tncolitem = new TreeNode();
                    tncolitem.Text = cols;
                    tncolitem.ImageIndex = 4;

                    tnCol.Nodes.Add(tncolitem);
                }
                tnDB.Nodes.Add(tnCol);

                TreeNode tnUsers = new TreeNode();
                tnUsers.Text = "Users";
                tnUsers.ImageIndex = 2;

                MongoUser[] mUsers = mServer.GetDatabase(db).FindAllUsers();

                foreach (MongoUser mUser in mUsers)
                {
                    TreeNode tnUser = new TreeNode();
                    tnUser.Text = mUser.Username;
                    tnUser.ImageIndex = 3;
                    tnUsers.Nodes.Add(tnUser);
                }

                tnDB.Nodes.Add(tnUsers);

                tnServer.Nodes.Add(tnDB);
            }


            frmSE.tvServerExplorer.Nodes.Add(tnServer);
            
        }
    }
}
